package org.zn.note.jdk.beans;

import org.apache.commons.beanutils.BeanUtils;

import java.beans.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName BeansTest
 * @Author zn
 * @Date 2019/8/30 14:56
 **/
public class BeansTest {

    // java.beans.XMLEncoder
    private static void beanToXml(Object bean, String fileName) {
        try (OutputStream outputStream = new FileOutputStream(fileName);
             XMLEncoder encoder = new XMLEncoder(outputStream);) {
            encoder.writeObject(bean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // java.beans.XMLDecoder
    private static Object xmlToBean(String fileName) {
        Object o = null;
        try (InputStream inputStream = new FileInputStream(fileName);
             XMLDecoder decoder = new XMLDecoder(inputStream);) {
            o = decoder.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return o;
    }

    // java.beans.Introspector
    private static <T> T mapToBean(Map map, Class<T> c) throws IllegalAccessException, InstantiationException {
        T instance = c.newInstance();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(c);
            PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor pd : pds) {
                String name = pd.getName(); // bean属性
                Object value = map.get(name);
                if (null != value) {
                    Method writeMethod = pd.getWriteMethod();
                    writeMethod.invoke(instance, value);
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return instance;
    }

    // org.apache.commons.beanutils.BeanUtils
    private static <T> T mapToBean2(Map map, Class<T> c) throws Exception {
        T bean = c.newInstance();
        BeanUtils.populate(bean, map);
        BeanUtils.setProperty(bean, "age", 18);
        String userId = org.apache.commons.beanutils.BeanUtils.getProperty(bean, "userId");
        System.out.println("userId:" + userId);
        return bean;
    }

    // org.springframework.beans.BeanUtils
    // 这个方法实际是不行的，Spring的BeanUtils好像不能讲Map转成Bean
//    private static <T> T mapToBean3(Map map, Class<T> c) throws Exception {
//        T bean = c.newInstance();
//        org.springframework.beans.BeanUtils.copyProperties(map, bean);
//        return bean;
//    }

    public static void main(String[] args) throws Exception {
        Map map = new HashMap();
        Long userId = 111L;
        map.put("userId", userId);
        map.put("userName", "Jack");
        User user = mapToBean(map, User.class);
        System.out.println(user.getUserId());
        userId = 222L;
        System.out.println(user.getUserId());
    }
}
